﻿namespace KeRui.Common
{
    using Microsoft.Win32;
    using System;

    public class WinReg
    {
        private static RegistryKey rootkey;

        public WinReg(string RootKey)
        {
            switch (RootKey.ToUpper())
            {
                case "CLASSES_ROOT":
                    rootkey = Registry.ClassesRoot;
                    break;

                case "CURRENT_USER":
                    rootkey = Registry.CurrentUser;
                    break;

                case "LOCAL_MACHINE":
                    rootkey = Registry.LocalMachine;
                    break;

                case "USERS":
                    rootkey = Registry.Users;
                    break;

                case "CURRENT_CONFIG":
                    rootkey = Registry.CurrentConfig;
                    break;

                case "DYN_DATA":
                    rootkey = Registry.DynData;
                    break;

                case "PERFORMANCE_DATA":
                    rootkey = Registry.PerformanceData;
                    break;

                default:
                    rootkey = Registry.CurrentUser;
                    break;
            }
        }

        public RegistryKey CreateRegKey(string keypath)
        {
            try
            {
                return rootkey.CreateSubKey(keypath);
            }
            catch
            {
                return null;
            }
        }

        public bool DelRegKeyVal(string keypath, string keyname)
        {
            try
            {
                rootkey.OpenSubKey(keypath, true).DeleteValue(keyname);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool DelRegSubKey(string keypath)
        {
            try
            {
                rootkey.DeleteSubKey(keypath);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool DelRegSubKeyTree(string keypath)
        {
            try
            {
                rootkey.DeleteSubKeyTree(keypath);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public string GetRegVal(string keypath, string keyname, string def)
        {
            try
            {
                return rootkey.OpenSubKey(keypath).GetValue(keyname, def).ToString();
            }
            catch
            {
                return def;
            }
        }

        public bool SetRegVal(string keypath, string keyname, string keyval)
        {
            try
            {
                rootkey.OpenSubKey(keypath, true).SetValue(keyname, keyval);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

